﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data;
using IUI;
using System.Data;

namespace BL
{
    public class AdminPageController : BaseController<IAdminPage>
    {
        public AdminPageController(IAdminPage page) : base(page) 
        {
            WCF_Client.Instance.Connect();
        }

        public override void InitPageEvents()
        {
            // подписываемся на событие показа страницы, чтобы отобразить таблицы
            _page.Page_Shown += Page_Shown;

            _page.Click_EditSongButton += Edit_Song;
            _page.Click_AddSongButton += Add_Song;
            _page.Click_DelSongButton += Del_Song;
            _page.Click_OkSongButton += Ok_Song;

            _page.Click_EditUserButton += Edit_User;
            _page.Click_AddUserButton += Add_User;
            _page.Click_DelUserButton += Del_User;
            _page.Click_OkUserButton += Ok_User;
        }

        public void Page_Shown(object sender, EventArgs e)
        {
            // Показываем талблицу пользователей
            _page.Users = WCF_Client.Instance.GetTable(User.TableName);

            // Показываем талблицу песен
            _page.Songs = WCF_Client.Instance.GetTable(Song.TableName);
        }

        #region Songs stuff
        public void Edit_Song(object sender, EventArgs e)
        {
            _page.Songs_Command = "UPDATE";
            _page.EnableSongInputs();
        }

        public void Del_Song(object sender, EventArgs e)
        {
            _page.Songs_Command = "DELETE";
            _page.EnableSongInputs();

            WCF_Client.Instance.DeleteFromTable(Song.TableName, _page.Current_Song);

            _page.DisableSongInputs();
        }

        public void Add_Song(object sender, EventArgs e)
        {
            _page.Songs_Command = "INSERT";
            _page.EnableSongInputs();
        }

        public void Ok_Song(object sender, EventArgs e)
        {
            switch (_page.Songs_Command)
            {
                case "UPDATE":
                    WCF_Client.Instance.UpdateTable(Song.TableName, _page.Current_Song);
                    break;
                case "INSERT":
                    WCF_Client.Instance.InsertToTable(Song.TableName, _page.Current_Song);
                    break;
                case "DELETE":
                    break;
                default:
                    break;
            }

            _page.DisableSongInputs();
        } 
        #endregion

        #region Users stuff
        public void Edit_User(object sender, EventArgs e)
        {
            _page.Users_Command = "UPDATE";
            _page.EnableUserInputs();
        }

        public void Del_User(object sender, EventArgs e)
        {
            _page.Users_Command = "DELETE";
            _page.EnableUserInputs();

            WCF_Client.Instance.DeleteFromTable(User.TableName, _page.Current_User);

            _page.DisableUserInputs();
        }

        public void Ok_User(object sender, EventArgs e)
        {
            // проверка на пустые поля пароля и логина
            DataTable tmp = _page.Current_User;

            if (null != tmp)
            {
                switch (_page.Users_Command)
                {
                    case "UPDATE":
                        WCF_Client.Instance.UpdateTable(User.TableName, tmp);
                        break;
                    case "INSERT":
                        WCF_Client.Instance.InsertToTable(User.TableName, tmp);
                        break;
                    case "DELETE":
                        break;
                    default:
                        break;
                }

                _page.DisableUserInputs();
            }
            else
            {
                _page.ErrorUser();
            }
        }

        public void Add_User(object sender, EventArgs e)
        {
            _page.Users_Command = "INSERT";
            _page.EnableUserInputs();
        }
        #endregion

    }
}
